package com.bh.ly.oa.adapter;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bh.ly.oa.R;
import com.bh.ly.oa.util.Config;

public class OrderListAdapter extends BaseAdapter {
	Context context = null;
	List<JSONObject> array ;

	public OrderListAdapter(Context contex) {
		this.context = contex;
	}
	
	public List<JSONObject> getArray() {
		return array;
	}

	public void setArray(List<JSONObject> array) {
		this.array = array;
	}
	
	public void setDatas(List<JSONObject> jsonList){
		this.array = jsonList;
	}

	@Override
	public int getCount() {
		return array.size();
	}

	@Override
	public Object getItem(int position) {
		return array.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		OrderItem orderItem = null;
		if (convertView == null) {
			orderItem = new OrderItem();
			convertView = LayoutInflater.from(context).inflate(R.layout.order_item, null);
		} else {
			orderItem = (OrderItem) convertView.getTag();
		}
		
		JSONObject jsonObject = (JSONObject) array.get(position);
		orderItem.jsonobject=jsonObject;
		initView(position,convertView,orderItem);
		try {
			orderItem.title.setText(jsonObject.getString("suppliername"));
			orderItem.ordercreatetime.setText(getDatetext(jsonObject.get("recorddate")));
			orderItem.recorder.setText(jsonObject.getString("recorder"));
			orderItem.status.setText(Config.getStatus(Integer.parseInt(jsonObject.getString("orderflag"))));
			//TODO:need add 
			orderItem.paytype.setText("打款发货");
			orderItem.type.setText("订货单");
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return convertView;
	}
	
	public String getDatetext(Object ob){
		if(ob==null || ob.toString().equals("null")){
			return "";
		}else{
			return  ob.toString().substring(0, 10);
		}
	}
	
    private void initView(int position,View convertView,OrderItem orderItem){
        
		orderItem.title = (TextView) convertView.findViewById(R.id.order_title);
		orderItem.ordercreatetime = (TextView) convertView.findViewById(R.id.createtime);
		orderItem.recorder=(TextView) convertView.findViewById(R.id.recorder);
		orderItem.status=(TextView) convertView.findViewById(R.id.text4);
		orderItem.paytype=(TextView) convertView.findViewById(R.id.text6);
		orderItem.type=(TextView) convertView.findViewById(R.id.text8);
        convertView.setTag(orderItem);
    }
	class OrderItem {
		TextView title;
		TextView ordercreatetime;
		TextView recorder;
		TextView status;
		TextView paytype;
		TextView type;
		JSONObject jsonobject;
	}

}
